int cmp(int* x, int* y)
{
    return *x - *y;
}

int* sortByBits(int* arr, int arrSize, int* returnSize) {
    int* a = (int*)malloc(sizeof(int) * arrSize);
    int* b = (int*)malloc(sizeof(int) * arrSize);
    qsort(arr, arrSize, sizeof(int), cmp);
    for (int i = 0; i < arrSize; i++)
    {
        a[i] = 0;
    }
    for (int i = 0; i < arrSize; i++)
    {
        int n = arr[i];
        while (n)
        {
            if ((n & 1) == 1)
            {
                a[i]++;
            }
            n = n >> 1;
        }
    }
    for (int i = 0; i < arrSize; i++)
    {
        printf("%d  ", a[i]);
    }
    int m = 0, r = 0;
    for (int i = 0; i < arrSize; i++)
    {
        int min = 500;
        for (int j = 0; j < arrSize; j++)
        {
            if ((a[j] > -1) && (min > a[j]))
            {
                min = a[j];
                m = j;
            }
        }
        a[m] = -1;
        b[r++] = arr[m];
    }
    *returnSize = arrSize;
    return b;
}